<?php

/**
 * @author Sven-Ove Bjerkan
 * @copyright 2008
 */

class DB {
	private $dbHost = 'localhost';
	private $dbUser = 'mmandal_skole';
	private $dbPass = 'u43ayBt7';
	private $dbName = 'mmandal_skole';
	
	private static $instance;
	private $con = null;
	
	/**
	 * The singleton method.
	 * @return The instance of the config-class.
	 */
    public static function singleton() {
        if (!isset(self::$instance)) {
            $c = __CLASS__;
            self::$instance = new $c;
        }

        return self::$instance;
    }
    
    /** 
     * Get the connection.
     * @return The connection
     */
     public function getCon() {
     	if (is_null($this->con)) $this->connect();
     	return $this->con;
     }
     
     /** 
      * Secure a query (escape strings, make sure int are int, etc) and return it
      * @param String $query The query
      * @param Array $types Array with the types of input ("i"=int, "s"=string)
      * @param Array $values Array with the values
      * @return The query
      */
      public function getQuery($query, array $types, array $values) {
      		$tmp = explode('?', $query);
      		$query = $tmp[0];
      		for ($i = 0; $i < sizeof($values); $i++) {
      			if ($types[$i] === 's') $query .= "'".mysqli_real_escape_string(
				  		$this->getCon(), $values[$i])."'";
      			else if ($types[$i] === 'i') $query .= (int)$values[$i];
      			else if ($types[$i] === 'd') $query .= (double)$values[$i];
      			else return false;
				$query .= $tmp[$i+1];
			} 
			
			return $query;
      }
      
    /** 
      * Execute a select-query
      * @param String $query The query
      * @return An array with key 'numRows' with numbers of rows and key 'result' with an array of the result
      */
      public function execSelectQuery($query) {
      		$res = mysqli_query($this->getCon(), $query);
      		if ($res !== false) {
	      		$return['numRows'] = mysqli_num_rows($res);
	      		$tmp = array();
	      		while ($row = mysqli_fetch_assoc($res)) {
	      			$tmp[] = $row;
	      		}
	      		$return['result'] = $tmp;
	      		return $return;
	      	}
	      	else return false;
      }
    
    /**
	 * Prevent users to clone the instance.
	 */
    public function __clone() {
        trigger_error('Clone is not allowed.', E_USER_ERROR);
    }
	
	/**
	 * Destructor.
	 * Closes the connection to the database.
	 */
	public function __destruct() {
		$this->closeConnection();
	}
	
	/**
	 * Connect to the database.
	 * @return boolean on connect.
	 */
	private function connect() {
		$this->con = mysqli_connect($this->dbHost, $this->dbUser, $this->dbPass, $this->dbName);
		if (!is_null($this->getCon())) return true;
		else return false;
	}
	
	/** 
	 * Close connection to the database.
	 */ 
	private function closeConnection() {
		if (!is_null($this->con)) {
			mysqli_close($this->getCon());
			$this->con = null;
		}
	}
	
	/**
	 * Set autocommit on/off.
	 * @param boolean $status true/false
	 * @return boolean of result
	 */
	public function setAutocommit($status) {
		if (!is_bool($status)) return false;
		return mysqli_autocommit($this->getCon(), $status);
	}
	
	/**
	 * Execute a delete-query.
	 * @param String $query The query.
	 * @return boolean of result
	 */
	public function execDeleteQuery($query) {
      	return mysqli_query($this->getCon(), $query);      		
    }
    
    /**
	 * Execute an insert-query.
	 * @param String $query The query.
	 * @return boolean of result
	 */
    public function execInsertQuery($query) {
    	// Uses deleteQuery, but having an separate function for each type of query makes it easy to add extra
		// features to spesific query-types later. 
      	return $this->execDeleteQuery($query);      		
    }
    
    /**
	 * Execute an update-query.
	 * @param String $query The query.
	 * @return boolean of result
	 */
    public function execUpdateQuery($query) {
    	// Uses deleteQuery, but having an separate function for each type of query makes it easy to add extra
		// features to spesific query-types later.
      	return $this->execDeleteQuery($query);      		
    }
    
	/**
	 * Commit queries
	 * @return boolean of result
	 */
    public function commit() {
    	return mysqli_commit($this->getCon());
    }
    
	/**
	 * Perform rollback
	 * @return boolean of result
	 */
    public function rollback() {
    	return mysqli_rollback($this->getCon());
    }
}

?>